package com.tuanzi.loan.business.repository.family.impl;

import com.tuanzi.loan.business.entity.family.LoanChannelNdInfo;
import com.tuanzi.loan.business.entity.family.LoanLargeAmountPartnerControl;
import com.tuanzi.loan.business.repository.family.custom.LoanLargeAmountPartnerControlRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.family.LoanLargeAmountPartnerControlSearchRequest;
import com.tuanzi.loan.vo.response.family.LoanLargeAmountPartnerControlSearchResponse;
import com.tuanzi.server.loan_base.model.db.LoanWholeSaleComment;
import com.tuanzi.server.loan_base.util.DateUtils;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.util.List;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanChannelNdInfo.LOAN_CHANNEL_ND_INFO;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanLargeAmountPartnerControl.LOAN_LARGE_AMOUNT_PARTNER_CONTROL;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanWholesaleComment.LOAN_WHOLESALE_COMMENT;

@Repository
public class LoanLargeAmountPartnerControlRepositoryImpl extends JOOQBaseRepositoryPlus implements LoanLargeAmountPartnerControlRepositoryCustom {
    @Override
    public Page<LoanLargeAmountPartnerControlSearchResponse> list(LoanLargeAmountPartnerControlSearchRequest searchRequest, Pageable pageable) {
        fields();
        eq(LOAN_LARGE_AMOUNT_PARTNER_CONTROL.USERNAME,searchRequest.getUsername());
        eq(LOAN_LARGE_AMOUNT_PARTNER_CONTROL.PARTNERTYPE,searchRequest.getPartnertype());
        return search(pageable,record -> {
            LoanLargeAmountPartnerControlSearchResponse response = new LoanLargeAmountPartnerControlSearchResponse();
            response.setUsername(record.get(LOAN_LARGE_AMOUNT_PARTNER_CONTROL.USERNAME));
            response.setPartnertype(record.get(LOAN_LARGE_AMOUNT_PARTNER_CONTROL.PARTNERTYPE));
            response.setPartnername(record.get(LOAN_LARGE_AMOUNT_PARTNER_CONTROL.PARTNERNAME));
            response.setIscommit(record.get(LOAN_LARGE_AMOUNT_PARTNER_CONTROL.ISCOMMIT));
            response.setIsapprove(record.get(LOAN_LARGE_AMOUNT_PARTNER_CONTROL.ISAPPROVE));
            response.setIssuccess(record.get(LOAN_LARGE_AMOUNT_PARTNER_CONTROL.ISSUCCESS));
            response.setUpdator(record.get(LOAN_LARGE_AMOUNT_PARTNER_CONTROL.UPDATOR));
            response.setUpdatetime(record.get(LOAN_LARGE_AMOUNT_PARTNER_CONTROL.UPDATE_TIME));
            response.setId(record.get(LOAN_LARGE_AMOUNT_PARTNER_CONTROL.ID));
            response.setCreatetime(record.get(LOAN_LARGE_AMOUNT_PARTNER_CONTROL.CREATE_TIME));
            response.setCreatetor(record.get(LOAN_LARGE_AMOUNT_PARTNER_CONTROL.CREATOR));
            response.setCfrom(record.get(LOAN_LARGE_AMOUNT_PARTNER_CONTROL.CFROM));
            response.setChannel_n(record.get(LOAN_LARGE_AMOUNT_PARTNER_CONTROL.CHANNEL_N));
            response.setIsDistinct(record.get(LOAN_LARGE_AMOUNT_PARTNER_CONTROL.ISDISTINCT));
            return  response;
        });
    }

    @Override
    public LoanWholeSaleComment judgeCfrom(Integer type) {
       List<LoanWholeSaleComment> loanlist =
              dslContext
                       .select(LOAN_WHOLESALE_COMMENT.WAITUI,LOAN_WHOLESALE_COMMENT.ZHANNEI)
                       .from(LOAN_WHOLESALE_COMMENT)
                .where(LOAN_WHOLESALE_COMMENT.TYPE.eq(type)).fetchInto(LoanWholeSaleComment.class);
        if (loanlist.size()>0){
            return loanlist.get(0);
        }
       return null;
    }

    @Override
    public void updateById(LoanLargeAmountPartnerControlSearchRequest request) {
        dslContext.update(LOAN_LARGE_AMOUNT_PARTNER_CONTROL)
                .set(LOAN_LARGE_AMOUNT_PARTNER_CONTROL.PARTNERTYPE,request.getPartnertype())
                .set(LOAN_LARGE_AMOUNT_PARTNER_CONTROL.USERNAME,request.getUsername())
                .set(LOAN_LARGE_AMOUNT_PARTNER_CONTROL.PARTNERNAME,request.getPartnername())
                .set(LOAN_LARGE_AMOUNT_PARTNER_CONTROL.ISCOMMIT,request.getIscommit())
                .set(LOAN_LARGE_AMOUNT_PARTNER_CONTROL.ISAPPROVE,request.getIsapprove())
                .set(LOAN_LARGE_AMOUNT_PARTNER_CONTROL.ISSUCCESS,request.getIssuccess())
                .set(LOAN_LARGE_AMOUNT_PARTNER_CONTROL.UPDATOR,request.getUpdator())
                .set(LOAN_LARGE_AMOUNT_PARTNER_CONTROL.UPDATE_TIME, Timestamp.valueOf(DateUtils.getCurrentTime()))
                .set(LOAN_LARGE_AMOUNT_PARTNER_CONTROL.CFROM,request.getCfrom())
                .set(LOAN_LARGE_AMOUNT_PARTNER_CONTROL.CHANNEL_N,request.getChannel_n())
                .set(LOAN_LARGE_AMOUNT_PARTNER_CONTROL.ISDISTINCT,request.getIsDistinct())
                .where(LOAN_LARGE_AMOUNT_PARTNER_CONTROL.ID.eq(request.getId()))
                .execute();

    }

    @Override
    public List<LoanWholeSaleComment> getPartnerInfo() {
        return dslContext.select(LOAN_WHOLESALE_COMMENT.TYPE,LOAN_WHOLESALE_COMMENT.OWNERNAME)
                .from(LOAN_WHOLESALE_COMMENT).fetchInto(LoanWholeSaleComment.class);
    }

    @Override
    public List<LoanLargeAmountPartnerControl> findAll() {
        return dslContext.select(LOAN_LARGE_AMOUNT_PARTNER_CONTROL.fields()).from(LOAN_LARGE_AMOUNT_PARTNER_CONTROL).fetchInto(LoanLargeAmountPartnerControl.class);
    }

    @Override
    public List<LoanChannelNdInfo> findAllChannelN() {
        return dslContext.select(LOAN_CHANNEL_ND_INFO.CHANNEL_ND).from(LOAN_CHANNEL_ND_INFO).fetchInto(LoanChannelNdInfo.class);
    }

    @Override
    protected TableImpl getTable() {
        return LOAN_LARGE_AMOUNT_PARTNER_CONTROL;
    }
}
